<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="等级名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gradeName">
             <!-- <a-input v-model="model.gradeName" placeholder="请输入等级名称"  ></a-input> -->
              {{model.gradeName}}
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="等级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="grade">
             <!-- <a-input v-model="model.grade" placeholder="请输入等级"  ></a-input> -->
              {{model.grade}}
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-card :bordered="false" title="一级佣金设置">
            <a-col :span="24">
              <a-form-model-item label="直推男用户金币充值(%)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="oneLevelManGold">
                <a-input-number :min="0" style="width: 80%" v-model="model.oneLevelManGold" placeholder="请输入直推男用户金币充值"  ></a-input-number>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="直推女用户社交收益(%)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="oneLevelWomanIncome">
                <a-input-number :min="0" style="width: 80%" v-model="model.oneLevelWomanIncome" placeholder="请输入直推女用户社交收益"  ></a-input-number>
              </a-form-model-item>
            </a-col>
          </a-card>
        </a-row>
      </a-form-model>
      <a-card :bordered="false" title="二级佣金设置">
        <a @click="addBtnClick" slot="extra" type="primary">添加规则</a>
        <span style="font-size: 12px;">当用户充值金币或产生社交收益时，对比此等级与旗下等级，按照表格中设定的佣金比例计算二级佣金</span>
        <a-table
          ref="table"
          size="middle"
          :scroll="{x:true}"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :loading="loading"
          class="j-table-force-nowrap"
          >
          <span slot="action" slot-scope="text, record">
          <a-popconfirm title="确定删除吗?" @confirm="() => deleteItem(record)">
            <a>删除</a>
          </a-popconfirm>
          <a-divider type="vertical" />
          <a @click="updateBtnClick(record)">修改</a>
        </span>

        </a-table>
      </a-card>
      <Reward-Two-Modal @itemAdd="itemAdd" @itemUpdate="itemUpdate" ref="RewardTwoModal"></Reward-Two-Modal>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import RewardTwoModal from './RewardTwoModal'

  export default {
    name: 'RewardRulesForm',
    components: {
      RewardTwoModal,
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           oneLevelManGold: [
              { required: true, message: '请输入一级佣金-直推男用户金币充值!'},
           ],
           oneLevelWomanIncome: [
              { required: true, message: '请输入一级佣金-直推女用户社交收益!'},
           ],
        },
        url: {
          add: "/system/rewardRules/add",
          edit: "/system/rewardRules/edit",
          queryById: "/system/rewardRules/queryById"
        },
        loading:false,
        dataSource:[],
        // 表头
        columns: [
          {
            title:'发生事件',
            align:"center",
            width:200,
            dataIndex: 'eventName'
          },
          {
            title:'旗下等级',
            align:"center",
            width:100,
            dataIndex: 'eventGrade'
          },
          {
            title:'二级佣金比例',
            align:"center",
            dataIndex: 'rate',
            customRender:function (t) {
              return t+"%";
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        if(record.id){
          this.getTwoList(record.id);
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            this.model.itemList=this.dataSource;
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },

      //添加规则
      addBtnClick(){
        if(this.model.grade !='1'){
          var item={};
          item.parentId=this.model.id;
          item.parentGrade=this.model.grade;
          this.$refs.RewardTwoModal.czType="1";
          this.$refs.RewardTwoModal.add(item);
        }else {
          this.$message.warning('一级会员不能设置二级佣金');
        }
      },

      //添加规则回显数据
      itemAdd(data){
        for(var i=0;i<this.dataSource.length;i++){
          if(this.dataSource[i].eventCode==data.eventCode && this.dataSource[i].eventGrade==data.eventGrade){
            this.$message.warning('该规则已存在');
            return;
          }
        }
        this.dataSource.push(data);
      },
      //更新数据
      itemUpdate(data){
        for(var i=0;i<this.dataSource.length;i++){
          if(this.dataSource[i].eventCode==data.eventCode && this.dataSource[i].eventGrade==data.eventGrade){
            this.dataSource[i].rate=data.rate;
            return;
          }
        }
      },

      //删除
      deleteItem(item){
        //this.dataSource.splice(index,1);
        this.dataSource = this.dataSource.filter(i => i !== item);
      },

      //修改
      updateBtnClick(item){
        this.$refs.RewardTwoModal.czType="2";
        this.$refs.RewardTwoModal.edit(item);
      },

      //初始化列表
      getTwoList(parentId){
        getAction("/system/rewardTwo/getList",{parentId:parentId}).then(res=>{
          if(res.success){
            this.dataSource=res.result;
          }else{
            this.$message.error(res.message)
          }
        })
      }

    }
  }
</script>